<template>
  <div>
    <router-link to="/shopcart">我的购物车</router-link>
    <router-link to="/shop">搜索页面</router-link>
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="createTime"
        label="生产日期"
        width="180">
      </el-table-column>
      <el-table-column
        prop="goodsName"
        label="商品名称"
        width="180">
      </el-table-column>
      <el-table-column
        prop="goodsDesc"
        label="商品描述">
      </el-table-column>
      <el-table-column
        prop="price"
        label="商品价格">
      </el-table-column>
      <el-table-column>
        <template slot-scope="scope">
          <el-button @click="addShpCart(scope.row)" type="text" size="small">添加到购物车</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  import axios from 'axios'
  axios.defaults.withCredentials=true;
  export default {
    data() {
      return {
        tableData: []
      }
    },
    methods:{
      findAll:function () {
        axios.get("http://localhost:9999/credit-goods/findAllGoods").then(res=>{
          if (res.data.code==200){
            this.tableData=  res.data.message
          }
        })
      },
      addShpCart:function (row) {
        axios.post("http://localhost:9999/credit-shopcart/addShopCart",{id:row.id}).then(res=>{
          if (res.data.code==200){
            const h = this.$createElement;
            this.$message({
              message: h('p', null, [
                h('i', { style: 'color: red' }, res.data.message)
              ])
            });
          }
        })
      }
    },
    mounted(){
      this.findAll();
    }
  }
</script>
